import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:mobile_app.home.core/core/presentation/theme/home_core_theme.dart';
import 'package:mobile_app.home.core/core/presentation/widgets/keyboard_text_field_divider.dart';
import 'package:mobile_app.home.core/core/presentation/widgets/shakeable_keyboard_text_field.dart';

class TypingCardKeyboardTextField extends StatefulWidget {
  const TypingCardKeyboardTextField({
    required this.controller,
    required this.focusNode,
    required this.onSubmitted,
    required this.correctAnswerColorAnimationController,
    required this.shakeAnimationController,
    required this.showCursor,
    super.key,
  });

  final TextEditingController controller;
  final FocusNode focusNode;
  final VoidCallback onSubmitted;
  final AnimationController correctAnswerColorAnimationController;
  final AnimationController shakeAnimationController;
  final bool showCursor;

  @override
  State<TypingCardKeyboardTextField> createState() => _TypingCardKeyboardTextFieldState();
}

class _TypingCardKeyboardTextFieldState extends State<TypingCardKeyboardTextField> {
  late Animation<Color?> _colorAnimation;

  late ColorTween _colorTween;

  Animation<Color?> _createColorAnimation({
    required AnimationController controller,
    required ColorTween colorTween,
  }) {
    return CurvedAnimation(
      parent: controller,
      curve: Curves.easeOutExpo,
    ).drive(colorTween);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();

    _colorTween = ColorTween(
      begin: HomeCoreTheme.of(context).textTheme.keyboardTextField.color!,
      end: HomeCoreTheme.of(context).cardCorrectAnswerTextColor,
    );

    _colorAnimation = _createColorAnimation(
      controller: widget.correctAnswerColorAnimationController,
      colorTween: _colorTween,
    );
  }

  @override
  void didUpdateWidget(covariant TypingCardKeyboardTextField oldWidget) {
    super.didUpdateWidget(oldWidget);

    if (oldWidget.correctAnswerColorAnimationController !=
        widget.correctAnswerColorAnimationController) {
      _colorAnimation = _createColorAnimation(
        controller: widget.correctAnswerColorAnimationController,
        colorTween: _colorTween,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        const KeyboardTextFieldDivider(),
        AnimatedBuilder(
          animation: _colorAnimation,
          builder: (context, _) {
            return ShakeableKeyboardTextField(
              controller: widget.controller,
              focusNode: widget.focusNode,
              onSubmitted: widget.onSubmitted,
              shakeAnimationController: widget.shakeAnimationController,
              textInputAction: TextInputAction.done,
              enableInteractiveSelection: true,
              enableSuggestions: false,
              autocorrect: false,
              showCursor: widget.showCursor,
              keyboardType: TextInputType.text,
              color: HomeCoreTheme.of(context).cardColor,
              textColor: _colorAnimation.value,
            );
          },
        ),
      ],
    );
  }
}
